#!/bin/bash
# Create animated GIF from PNG sequence

# Default values
INPUT_DIR="../run_scripts/output/visualization"
OUTPUT_FILE="dragonfly_simulation.gif"
FPS=10
SCALE=800  # Max width for GIF

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -i|--input)
            INPUT_DIR="$2"
            shift 2
            ;;
        -o|--output)
            OUTPUT_FILE="$2"
            shift 2
            ;;
        -f|--fps)
            FPS="$2"
            shift 2
            ;;
        -s|--scale)
            SCALE="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 [-i INPUT_DIR] [-o OUTPUT_FILE] [-f FPS] [-s SCALE]"
            echo "  -i, --input    Input directory with PNG files (default: ../../output/visualization)"
            echo "  -o, --output   Output GIF file (default: dragonfly_simulation.gif)"
            echo "  -f, --fps      Frames per second (default: 10)"
            echo "  -s, --scale    Max width in pixels (default: 800)"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            exit 1
            ;;
    esac
done

echo "======================================================"
echo "Creating animated GIF from dragonfly simulation"
echo "======================================================"
echo "Input directory: $INPUT_DIR"
echo "Output file: $OUTPUT_FILE"
echo "Frame rate: $FPS fps"
echo "Max width: $SCALE pixels"
echo "======================================================"

# Check if input directory exists
if [ ! -d "$INPUT_DIR" ]; then
    echo "Error: Input directory '$INPUT_DIR' does not exist"
    exit 1
fi

# Count PNG files
PNG_COUNT=$(ls -1 "$INPUT_DIR"/scene_*.png 2>/dev/null | wc -l)
if [ $PNG_COUNT -eq 0 ]; then
    echo "Error: No scene_*.png files found in $INPUT_DIR"
    exit 1
fi

echo "Found $PNG_COUNT frames"

# Method 1: Using FFmpeg (if available)
if command -v ffmpeg &> /dev/null; then
    echo "Creating GIF with FFmpeg..."
    
    # First create a palette for better colors
    ffmpeg -y -framerate $FPS -i "$INPUT_DIR/scene_%05d.png" \
        -vf "fps=$FPS,scale=$SCALE:-1:flags=lanczos,palettegen" \
        palette.png 2>/dev/null
    
    # Then create the GIF using the palette
    ffmpeg -y -framerate $FPS -i "$INPUT_DIR/scene_%05d.png" -i palette.png \
        -filter_complex "fps=$FPS,scale=$SCALE:-1:flags=lanczos[x];[x][1:v]paletteuse" \
        "$OUTPUT_FILE" 2>&1 | grep -E "frame=|fps=|time=|speed="
    
    # Clean up
    rm -f palette.png
    
# Method 2: Using ImageMagick (if available)
elif command -v convert &> /dev/null; then
    echo "Creating GIF with ImageMagick..."
    DELAY=$((100/$FPS))  # Convert fps to delay in centiseconds
    
    convert -delay $DELAY -loop 0 -resize ${SCALE}x \
        "$INPUT_DIR"/scene_*.png "$OUTPUT_FILE"
        
else
    echo "Error: Neither FFmpeg nor ImageMagick is installed"
    echo "Install one of them:"
    echo "  FFmpeg: sudo apt-get install ffmpeg"
    echo "  ImageMagick: sudo apt-get install imagemagick"
    exit 1
fi

# Check if successful
if [ -f "$OUTPUT_FILE" ]; then
    SIZE=$(ls -lh "$OUTPUT_FILE" | awk '{print $5}')
    
    echo ""
    echo "✓ GIF created successfully!"
    echo "  File: $OUTPUT_FILE"
    echo "  Size: $SIZE"
    echo "  Frames: $PNG_COUNT"
    echo ""
    echo "To view the GIF:"
    echo "  - Open in web browser: firefox $OUTPUT_FILE"
    echo "  - Open in image viewer: eog $OUTPUT_FILE"
else
    echo "✗ Error creating GIF"
    exit 1
fi 